<?php

/**
 *
 * @license   https://www.rongyun.cc
 * @link      https://www.rongyun.cc
 * @author    wangyadong<wangyadong280@gmail.com>
 * @date      2017-05-11 17:54
 */


namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;

class BaseController extends Controller
{

    protected function errorBadRequest($validator)
    {
        // github like error messages
        // if you don't like this you can use code bellow
        //
        //throw new ValidationHttpException($validator->errors());

        $result = [];
        $messages = $validator->errors()->toArray();

        if ($messages) {
            foreach ($messages as $field => $errors) {
                foreach ($errors as $error) {
                    $result[] = [
                        'field' => $field,
                        'code' => $error,
                    ];
                }
            }
        }
        throw new ValidationHttpException($result);
    }


    protected function apiResponse($data, $message = '', $code = 0)
    {
        return response()->json([
            'code'    => $code,
            'status'  => $code == 0 ? 'success' : 'error',
            'message' => $message,
            'data'    => $data,
        ]);
    }
}